
/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.log.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.oms.log.domain.OmsCommonOrderLog;
import com.lxk.oms.log.domain.bo.OmsCommonOrderLogBo;
import com.lxk.oms.log.domain.vo.OmsCommonOrderLogVo;
import com.lxk.oms.log.mapper.OmsCommonOrderLogMapper;
import com.lxk.oms.log.service.IOmsCommonOrderLogService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通用订单日志Service业务层处理
 *
 * @author LXK-RY5
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
@DS("order")
public class OmsCommonOrderLogServiceImpl implements IOmsCommonOrderLogService {

    private final OmsCommonOrderLogMapper baseMapper;

    /**
     * 查询IdList通用订单日志Map
     */
    @Override
    public Map<Long, OmsCommonOrderLogVo> queryMapByIdList(List<Long> idList) {
        return this.queryByIdList(idList).stream().collect(Collectors.toMap(OmsCommonOrderLogVo::getId, v -> v));
    }

    /**
     * 查询通用订单日志Map
     */
    @Override
    public Map<Long, OmsCommonOrderLogVo> queryMap(OmsCommonOrderLogBo bo) {
        return this.queryList(bo).stream().collect(Collectors.toMap(OmsCommonOrderLogVo::getId, v -> v));
    }

    /**
     * 查询通用订单日志IdList
     */
    @Override
    public List<OmsCommonOrderLogVo> queryByIdList(List<Long> idList) {
        if (CollUtil.isEmpty(idList)) {
            return CollUtil.newArrayList();
        }
        LambdaQueryWrapper<OmsCommonOrderLog> lqw = Wrappers.lambdaQuery();
        lqw.in(OmsCommonOrderLog::getId, idList);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询通用订单日志
     *
     * @param id 主键
     * @return 通用订单日志
     */
    @Override
    public OmsCommonOrderLogVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询通用订单日志列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 通用订单日志分页列表
     */
    @Override
    public TableDataInfo<OmsCommonOrderLogVo> queryPageList(OmsCommonOrderLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OmsCommonOrderLog> lqw = buildQueryWrapper(bo);
        Page<OmsCommonOrderLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的通用订单日志列表
     *
     * @param bo 查询条件
     * @return 通用订单日志列表
     */
    @Override
    public List<OmsCommonOrderLogVo> queryList(OmsCommonOrderLogBo bo) {
        LambdaQueryWrapper<OmsCommonOrderLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OmsCommonOrderLog> buildQueryWrapper(OmsCommonOrderLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OmsCommonOrderLog> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(OmsCommonOrderLog::getId);
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            OmsCommonOrderLog::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            OmsCommonOrderLog::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.eq(bo.getMerId() != null, OmsCommonOrderLog::getMerId, bo.getMerId());
        lqw.eq(bo.getUserId() != null, OmsCommonOrderLog::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), OmsCommonOrderLog::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getLogType()), OmsCommonOrderLog::getLogType, bo.getLogType());
        lqw.eq(StringUtils.isNotBlank(bo.getUserType()), OmsCommonOrderLog::getUserType, bo.getUserType());
        lqw.eq(StringUtils.isNotBlank(bo.getOmsSn()), OmsCommonOrderLog::getOmsSn, bo.getOmsSn());
        return lqw;
    }

    /**
     * 新增通用订单日志
     *
     * @param bo 通用订单日志
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OmsCommonOrderLogBo bo) {
        if (ObjectUtil.isNull(bo.getUserId())) {
            return false;
        }
        OmsCommonOrderLog add = MapstructUtils.convert(bo, OmsCommonOrderLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改通用订单日志
     *
     * @param bo 通用订单日志
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OmsCommonOrderLogBo bo) {
        OmsCommonOrderLog update = MapstructUtils.convert(bo, OmsCommonOrderLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OmsCommonOrderLog entity) {
    }

    /**
     * 校验并批量删除通用订单日志信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
